﻿/**
*	Sprite3D
*	@author 	Liam O'Donnell 2007-12-18
**/

package com.spikything.pants3d.objects {
	
	import com.spikything.pants3d.Pants3D;
	import com.spikything.pants3d.core.Point3D;
	import com.spikything.pants3d.objects.IRenderable;
	import flash.display.Sprite;
	
	public class Sprite3D extends Sprite implements IRenderable {
		
		public var pos				:Point3D;		// Position object
		public var vx				:Number;		// X speed
		public var vy				:Number;		// Y speed
		public var vz				:Number;		// Z speed
		public var fx 				:Number 		// X driving force
		public var fy 				:Number 		// Y driving force
		public var fz 				:Number 		// Z driving force
		public var isVisible		:Boolean;		// Visibility of sprite in 3-D viewport
		public var spriteType		:String;		// Optional identifier used for iteration
		public var child			:Sprite;		// A sprite passed into this container
		public var selfLighting 	:Object;		// Self illumination RGB components
		public var shadow 			:Plane3D;		// Optional object shadow
		protected var _id 			:String 		// A readable id/name tag for the object

		public function Sprite3D (x:Number = 0, y:Number = 0, z:Number = 0, child:Sprite = null) {
			_id = "Sprite3D";
			mouseEnabled = mouseChildren = false;
			pos = new Point3D(0, 0, 0);
			setPos(x, y, z);
			vx = vy = vz = 0;
			fx = fy = fz = 0;
			isVisible = true;
			spriteType = Pants3D.NORMAL;
			if (child) addChild(child);
			this.child = child;
			selfLighting = {r:0, g:0, b:0};
		}
		
		public override function toString ():String { return "Sprite3D" };
		public function get id ():String { return _id };
		public function get isCollidable ():Boolean { return false };
		
		public function get px ():Number { return pos.px };
		public function get py ():Number { return pos.py };
		public function get pz ():Number { return pos.pz };
		public function set px (x:Number):void { pos.px = x };
		public function set py (y:Number):void { pos.py = y };
		public function set pz (z:Number):void { pos.pz = z };

		public function update ():void {
			vx += fx;
			vy += fy;
			vz += fz;
			px += vx;
			py += vy;
			pz += vz;
		}

		public function impulse (ix:Number, iy:Number, iz:Number):void {
			vx += ix;
			vy += iy;
			vz += iz;
		}

		public function setVelocities (ix:Number, iy:Number, iz:Number):void {
			vx = ix;
			vy = iy;
			vz = iz;
		}

		public function setPos (ix:Number, iy:Number, iz:Number):void {
			px = ix;
			py = iy;
			pz = iz;
		}
		
		public function getSelfLighting (channel:String):Number {
			return selfLighting[channel];
		}
		
		public function addShadow (shadow:Plane3D):void {
			this.shadow = shadow;
		}
		
	}
	
}